//
//  WBHome.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/9/26.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit
import SDWebImage

private let originalCellId = "originalCellId"
private let retweetedCellId = "retweedCellId"

class WBHomeViewController: WBBaseViewController {
    
//    lazy var dataList = [String]()
    
    lazy var statusListView = WBStatusListViewModel()
    
    override func loadData() {
        
        DispatchQueue.global().async {
            
           self.statusListView.loadStatus(isPull: self.isPull,completion: { (isSuccess, hasMoreData) in
            
            self.isPull = false
            if hasMoreData && isSuccess {
                
               self.tableView?.reloadData()
                
            }
            self.refershControl?.endRefreshing()
           })

        }
    }
}


extension WBHomeViewController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListView.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let viewModel = statusListView.statusList[indexPath.row]
        
        let cellId = (viewModel.status.retweeted_status == nil) ? originalCellId : retweetedCellId
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        
        cell.viewModel = viewModel
        
        return cell
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        
//    }
    
}


extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        naviItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        //tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        tableView?.separatorStyle = .none
        setUpNaviTitle()
    }
    
    
    func showFriends()  {
        let vc = WBTestViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    
    func setUpNaviTitle() {
        
        let title = WBNetworkingManager.shared.userAccount.screen_name
        
        let btn = WBTitleButton(title: title)
        
        btn.addTarget(self, action: #selector(titleBtnClick), for: .touchUpInside)
        
        self.naviItem.titleView = btn;
    }
    
    @objc func titleBtnClick(btn: UIButton) {
        
        btn.isSelected = !btn.isSelected
    }
}
